package com.reny.szkj.ft_login.ui.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.reny.ll.git.base_logic.MViewModel
import com.reny.ll.git.base_logic.api.exceptionProcess
import com.reny.ll.git.base_logic.model.login.LoginInfo
import com.reny.ll.git.base_logic.model.login.save
import com.reny.ll.git.base_logic.showLoading

class LoginViewModel(val repo: LoginRepository) : MViewModel() {

    val result: MutableLiveData<LoginInfo> = MutableLiveData()

    fun login(phone: String, pwd: String){
        showLoading()
        launch(exceptionProcess()) {
            repo.login(phone, pwd).apply {
                this.save()
                result.postValue(this)
            }
        }
    }

}


@Suppress("UNCHECKED_CAST")
class LoginViewModelFactory(
    private val repo: LoginRepository
) : ViewModelProvider.Factory {

    override fun <T : ViewModel?> create(modelClass: Class<T>): T = LoginViewModel(repo) as T

}